package main

import (
	"fmt"
	"github.com/zhan3333/glog"
	"os"
	"time"

	"server/src/zyplog"
)

/*
//全局日志变量
var g_log *log.Logger

//初始化全局变量
func initGlobal() {
	g_log, _ = syslog.NewLogger(syslog.LOG_NOTICE, 0)

}

//全局日志函数
func zLog(conn *net.Conn, args ...interface{}) {
	var sIP string

	if conn != nil {
		sIP = (*conn).RemoteAddr().String()
	} else {
		sIP = ""
	}
	sAtt := fmt.Sprint(args...)
	g_log.Println(sIP, sAtt)
}
*/
//全局日志变量
var g_log *zyplog.ZypLoger

// 初始化全局变量
func initGlobal() {
	if len(os.Args) > 1 {
		if os.Args[1] == "-v" {
			fmt.Println(VER)
			os.Exit(0)
		}
	}
}

// 全局日志函数
func zLog(args ...interface{}) {
	glog.Channel("tiredataprocessor2d").Infoln(args)
}

func zLogError(args ...interface{}) {
	glog.Channel("tiredataprocessor2d").Errorln(args...)
}

// 计算时间差
func calcDuration(st, et int) int { // (h, m, s int) {
	sH := st / 10000
	sM := st % 10000 / 100
	sS := st % 100
	sTS := sS + sM*60 + sH*60*60

	eH := et / 10000
	eM := et % 10000 / 100
	eS := et % 100
	eTS := eS + eM*60 + eH*60*60

	if eTS < sTS {
		eTS += 24 * 60 * 60
	}

	// d := eTS - sTS
	// h = d / 3600
	// m = d % 3600 / 60
	// s = d % 60

	return eTS - sTS
}

// 时间间隔
func intTimeToTime(dt int) time.Time {
	d := dt / 1000000
	t := dt % 1000000

	year := d / 10000
	month := d % 10000 / 100
	day := d % 100
	hour := t / 10000
	minute := t % 10000 / 100
	second := t % 100

	ret := time.Date(year, time.Month(month), day, hour, minute, second, 0, time.Now().Location())
	return ret
}

func intTimeToDuration(timeBegin, timeEnd int) int {
	if timeBegin == 0 || timeEnd == 0 {
		return 0
	}

	dt1 := intTimeToTime(timeBegin)
	dt2 := intTimeToTime(timeEnd)

	dur := dt2.Sub(dt1)
	return int(dur.Seconds())
}

// 整型时间转时间戳
func intTimeToTimestamp(dt int64) int64 {
	//200610182619
	if dt < 1000000000000 {
		dt += 20000000000000
	}
	tt := intTimeToTime(int(dt))
	return tt.Unix()
}
